#include <iostream>
using namespace std;

const int col = 5;
enum dir
{
    Asc,
    Desc
};

void sort(int a[][col], int n, int Cn, dir D)
{
    int t[col];
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if ((a[i][Cn] < a[j][Cn] && D == Desc) || (a[i][Cn] > a[j][Cn] && D == Asc))
            {
                memcpy(t, a[i], sizeof(t));
                memcpy(a[i], a[j], sizeof(t));
                memcpy(a[j], t, sizeof(t));
            }
        }
    }
}

void quick(int *a, int i, int j)
{
    int m, n, temp;
    int k;
    m = i;
    n = j;
    k = a[(i + j) / 2]; /*选取的参照*/
    do
    {
        while (a[m] < k && m < j)
            m++; /* 从左到右找比k大的元素*/
        while (a[n] > k && n > i)
            n--; /* 从右到左找比k小的元素*/
        if (m <= n)
        { /*若找到且满足条件，则交换*/
            temp = a[m];
            a[m] = a[n];
            a[n] = temp;
            m++;
            n--;
        }
    } while (m <= n);
    if (m < j)
        quick(a, m, j); /*运用递归*/
    if (n > i)
        quick(a, i, n);
}

int main()
{
    const int CourseN = 5;
    int n, score[][CourseN] = {
               {20140101, 1, 82, 86, 0},
               {20140203, 2, 80, 80, 0},
               {20140204, 2, 86, 90, 0},
               {20140205, 2, 90, 83, 0},
               {20140102, 1, 75, 86, 0}};

    n = sizeof(score) / sizeof(score[0]);
    for (int i = 0; i <= CourseN; i++)
    {
        for (int j = 2; j < CourseN - 1; j++)
        {
            score[i][CourseN - 1] += score[i][j];
        }
    }

    sort(score, n, 4, Desc);
    sort(score, n, 1, Asc);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < CourseN; j++)
        {
            cout << score[i][j] << '\t';
        }
        cout << endl;
    }

    return 0;
}
